<template>
  <section>
    <el-form label-width="80px">
      <el-form-item label="名称">
        <el-input size="mini" style="width: 200px" v-model="detection.title"></el-input>
      </el-form-item>
      <el-form-item label="检测项">
        <detection-item :detection="detection"></detection-item>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="success" @click="confirmBtn(detection)">{{detection.id?'保存':'新建'}}</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
  import detectionItem from './item/detectionItem'
  import errHandler from '@/assets/errHandler.js'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  export default {
    name: "editDetection",
    props:['detection'],
    components:{detectionItem},
    data(){
      return{
      }
    },
    created(){
    },
    methods:{
      ...mapActions({
        fetch: 'fetch'
      }),
      confirmBtn(detection){
        console.log(detection)
        let data={title:detection.title,data:detection.data,companyId:detection.companyId}
        if(detection.id){
          data.id=detection.id
          this.updateDetection(data)
        }
        else{
          data.detectionItems=JSON.stringify(detection.detectionItems)
          this.createDetection(data)
        }
      },
      updateDetection(data){
        this.fetch({name:"updateDetection",data})
          .then(res=>{
            console.log('更新成功-------',res)
            this.$emit('cancelDialog')
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      createDetection(data){
        console.log(data)
        this.fetch({name:"createDetection",data})
          .then(res=>{
            console.log('创建成功-------',res)
            this.$emit('cancelDialog')
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      }
    }
  }
</script>

<style scoped>

</style>
